const express = require("express");
const pool = require("../pool.js");

function vonvert(res) {
	res = JSON.stringify(res[0]);
	res = JSON.parse(res);
	return res;
}


//使用理由器
var router = express.Router();
//增加
router.post("/add", (req, res) => {
	var obj = req.body;
	var sql = "insert into product set ?"
	pool.query(sql, obj, (err, result) => {
		if (err) throw err;
		if (result.affectedRows > 0) {
			res.send({
				"code": 200,
				"msg": "添加成功"
			});
		} else {
			res.send({
				"code": 301,
				"msg": "添加失败"
			});
		}
	})
})
//商品列表
router.get("/list", (req, res) => {
	var obj = req.query;
	var _page = obj.page;
	var _limit = obj.limit;
	if (!_page) {
		_page = 1;
	}
	if (!_limit) {
		_limit = 20;
	}
	//计算从第几条数据开始
	_page = (_page - 1) * _limit;
	//把字符串转化为整数
	_page = parseInt(_page);
	_limit = parseInt(_limit);

	var sql = "SELECT * FROM product LIMIT ?,?";
	pool.query(sql, [_page, _limit], (err, result) => {
		if (err) throw err;
		pool.query("SELECT COUNT(*) AS count FROM product;", (error, resu) => {
			if (error) throw error;
			resu = vonvert(resu);
			var count = resu.count;
			res.send({
				"code": 0,
				"count": count,
				"data": result
			});
		})
	});
});
//删除
router.get("/delete", (req, res) => {
	var _pid = req.query.pid;
	var sql = "DELETE FROM product WHERE pid=?";
	pool.query(sql, _pid, (err, result) => {
		if (err) throw err;
		if (result.affectedRows > 0) {
			res.send({
				"code": 200,
				"msg": "删除成功"
			});
		} else {
			res.send({
				"code": 301,
				"msg": "删除失败"
			});
		}
	})
})
//获取详情
router.get("/details", (req, res) => {
	var _pid = req.query.pid;
	var sql = "SELECT * FROM product WHERE pid=?";
	pool.query(sql, _pid, (err, result) => {
		if (err) throw err;
		if (result.length) {
			res.send(result);
		} else {
			res.send({
				"code": 301,
				"msg": "查询失败"
			})
		}
	})
})
//编辑
router.post("/update", (req, res) => {
	var obj = req.body;
	for (var key in obj) {
		if (!obj[key]) {
			res.send({
				"code": 301,
				"msg": key + "不能为空"
			})
			return;
		}
	}
	var _pid = obj.pid;
	delete obj.pid;
	var sql = "UPDATE product SET ? WHERE pid=?";
	pool.query(sql, [obj, _pid], (err, result) => {
		if (err) throw err;
		if (result.affectedRows > 0) {
			res.send({
				"code": 200,
				"msg": "修改成功"
			})
		} else {
			res.send({
				"code": 301,
				"msg": "修改失败"
			})
		}
	})
})
//按照商品类别查询商品列表带分页
router.get("/classification", (req, res) => {
	var obj = req.query;
	var _class_id = obj.id,
		_page = obj.page,
		_limit = obj.limit;
	if (!_page) {
		_page = 1;
	}
	if (!_limit) {
		_limit = 20;
	}
	//计算从第几条数据开始
	_page = (_page - 1) * _limit;
	//把字符串转化为整数
	_page = parseInt(_page);
	_limit = parseInt(_limit);

	var sql = "select * from product where class_id=? limit ?,?; "
	pool.query(sql, [_class_id, _page, _limit], (err, result) => {
		if (err) throw err;
		pool.query("SELECT COUNT(*) AS count FROM product WHERE class_id=?;", _class_id, (error, resu) => {
			if (error) throw error;
			resu = vonvert(resu);
			var count = resu.count;
			res.send({
				"code": 0,
				"count": count,
				"data": result
			});
		})
	})
})

//按球队查询
router.get("/lookup_team", (req, res) => {
	var obj = req.query;
	var _tid = obj.id,
		_page = obj.page,
		_limit = obj.limit;
	if (!_page) {
		_page = 1;
	}
	if (!_limit) {
		_limit = 20;
	}
	//计算从第几条数据开始
	_page = (_page - 1) * _limit;
	//把字符串转化为整数
	_page = parseInt(_page);
	_limit = parseInt(_limit);
	var sql = "select * from product where tid=? limit ?,?;"
	pool.query(sql, [_tid,_page,_limit], (err, result) => {
		if (err) throw err;
		pool.query("SELECT COUNT(*) AS count FROM product WHERE tid=?;",_tid , (error, resu) => {
			if (error) throw error;
			resu = vonvert(resu);
			var count = resu.count;
			res.send({
				"code": 0,
				"count": count,
				"data": result
			});
		})
	})
})

//商品详情
router.get("/index_details", (req, res) => {
	//获取商品id
	var _pid = req.query.pid;
	//获取商品详情
	pool.query("SELECT * FROM product WHERE pid=?", _pid, (err, result) => {
		if (err) throw err;
		result = vonvert(result);
		delete result.pimg;
		//获取商品详情图片
		pool.query("SELECT * FROM product_img WHERE pid=?", _pid, (img_err, img_result) => {
			if (img_err) throw img_err;
			result.pimg = img_result;
			pool.query("SELECT * FROM product_color", (color_err, color_result) => {
				if (color_err) throw color_err;
				result.color = color_result;
				pool.query("SELECT * FROM product_size", (size_err, size_result) => {
					if (size_err) throw size_err;
					result.size = size_result;
					res.send(result);
				})
			})
		})
	})
})




//导出路由器
module.exports = router;
